﻿/*

Copyright 2009 Guillaume Amringer

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

package {
	import flash.display.MovieClip;
	import LoadingBarOrientation;
	
	public class LoadingBar extends MovieClip {
		
		public var movingBar:MovieClip = new MovieClip();
		private var _orientation:String;
		
		private var _width:int;
		private var _height:int;
		
		public function LoadingBar(pWidth:int, pHeight:int, pOrientation:String = LoadingBarOrientation.AUTO):void {
			
			_width = pWidth;
			_height = pHeight;
			
			if (pOrientation == LoadingBarOrientation.AUTO)
				if (pWidth > pHeight)
					_orientation = LoadingBarOrientation.HORIZONTAL;
				else if (pWidth < pHeight)
					_orientation = LoadingBarOrientation.VERTICAL;
				else
					_orientation = LoadingBarOrientation.DEFAULT;
			else
				_orientation = pOrientation;
			
			this.graphics.lineStyle(1, 0xffffff);
			this.graphics.drawRect(0, 0, pWidth, pHeight);
			
			movingBar.graphics.beginFill(0xffffff);
			movingBar.graphics.drawRect(0, 0, pWidth, pHeight);
			movingBar.graphics.endFill();
			
			if(_orientation == LoadingBarOrientation.HORIZONTAL)
				movingBar.width = 0;
			else
				movingBar.height = 0;
			
			this.addChild(movingBar);
		}
		
		public function updatePercentage(pPercentage:Number):void {
			if(_orientation == LoadingBarOrientation.HORIZONTAL)
				movingBar.width = _width * pPercentage;
			else
				movingBar.height = _height * pPercentage;
		}
	}
}